package level.state;

import java.util.ArrayList;
import java.util.Iterator;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class StateWriter
{
	public static void write(State state)
	{
		try
		{
			DocumentBuilderFactory dbFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();

			Document doc = dBuilder.newDocument();
			Element rootElement = doc.createElement("Map");
			doc.appendChild(rootElement);

			Element nameEl = doc.createElement("Name");
			nameEl.appendChild(doc.createTextNode(state.getFile().getName()));
			rootElement.appendChild(nameEl);

			Element size = doc.createElement("Size");
			rootElement.appendChild(size);

			Iterator<String> iterator = StateHandler.getIterator(state);
			while (iterator.hasNext())
			{
				String layerName = iterator.next();
				StateLayer layer = StateHandler.getLayer(state, layerName);
				write(layer, doc, rootElement);
			}
			TransformerFactory transformerFactory = TransformerFactory
					.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			transformer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount", "4");
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(state.getFile());
			transformer.transform(source, result);
		}
		catch (Exception e)
		{

		}
	}

	private static void write(StateLayer layer, Document doc,
			Element rootElement)
	{
		if (layer instanceof StateTileLayer)
		{
			writeTileLayer((StateTileLayer) layer, doc, rootElement);
		}

	}

	private static void writeTileLayer(StateTileLayer layer, Document doc,
			Element rootElement)
	{
		String layerName = layer.getName();
		ArrayList<Tile> tiles = layer.getTiles();
		Element layerNameEl = doc.createElement(layerName);
		rootElement.appendChild(layerNameEl);
		if (tiles.size() != 0)
		{
			for (Tile tile : tiles)
			{
				Element tileEl = doc.createElement("Tile");
				layerNameEl.appendChild(tileEl);

				Element image = doc.createElement("Image");
				image.appendChild(doc.createTextNode(tile.getIconPath()));
				tileEl.appendChild(image);

				Element x = doc.createElement("x");
				x.appendChild(doc.createTextNode(Integer.toString(tile.getX())));
				tileEl.appendChild(x);

				Element y = doc.createElement("y");
				y.appendChild(doc.createTextNode(Integer.toString(tile.getY())));
				tileEl.appendChild(y);

				Element w = doc.createElement("w");
				w.appendChild(doc.createTextNode(Integer.toString(tile.getW())));
				tileEl.appendChild(w);

				Element h = doc.createElement("h");
				h.appendChild(doc.createTextNode(Integer.toString(tile.getH())));
				tileEl.appendChild(h);

				Element sx = doc.createElement("sx");
				sx.appendChild(doc.createTextNode(Integer.toString(tile.getSx())));
				tileEl.appendChild(sx);

				Element sy = doc.createElement("sy");
				sy.appendChild(doc.createTextNode(Integer.toString(tile.getSy())));
				tileEl.appendChild(sy);

				Element sw = doc.createElement("sw");
				sw.appendChild(doc.createTextNode(Integer.toString(tile.getSw())));
				tileEl.appendChild(sw);

				Element sh = doc.createElement("sh");
				sh.appendChild(doc.createTextNode(Integer.toString(tile.getSh())));
				tileEl.appendChild(sh);
			}
		}
	}

}
